//
//  MazeGameVC+Welcome.swift
//  MazeGame
//
//  Created by orienthong on 2018/12/21.
//  Copyright © 2018 OrientHong. All rights reserved.
//

import SceneKit

extension MazeGameVC {
    
    //animate sphere
    func animateSphere() {
        let (directions, _, _, _) = helper.Check(with: configurator.matrix, startPoint: configurator.entrance, endPoint: configurator.endPoint)
        if directions.count > 0 {
            let directionQueue = helper.getAnimationQueue(with: directions)
            let action = helper.getSequenceActions(from: directionQueue)
            //we should  switch the directions
            let switchDirectionQueue = helper.reverseAll(directions: directionQueue)
            var actions = helper.getActions(form: switchDirectionQueue)
            actions.reverse()
            
            //reversed the directionQueue so it will go back
            let backAction = SCNAction.sequence(actions)
            
            let repeatAction = SCNAction.sequence([action, backAction])
            sphereNode.runAction(SCNAction.repeatForever(repeatAction))
        }
    }
    func hideLevelNode() {
        let levelNode = self.gameScene.rootNode.childNode(withName: "level", recursively: true)!
        let animateQueue = [["test1"],
                            ["test2", "test6"],
                            ["test3", "test7", "test11"],
                            ["test4", "test8", "test12", "test16"],
                            ["test5", "test9", "test13", "test17", "test21"],
                            ["test10", "test14", "test18", "test22"],
                            ["test15", "test19", "test23"],
                            ["test20", "test24"],
                            ["test25"]]
        delay(delay: 0, closure: {
            for i in 0 ..< animateQueue.count {
                delay(delay: 1.0 * Double(i) / Double(animateQueue.count), closure: {
                    for j in 0 ..< animateQueue[i].count {
                        let node = levelNode.childNode(withName: animateQueue[i][j], recursively: false)!
                        SCNTransaction.animateWithDuration(0.5, completionBlock: {
                            SCNTransaction.animateWithDuration(0.25) {
                                node.position.y -= 0.2
                            }
                        }, animations: {
                            node.position.y += 1.2
                            node.opacity = 0.0
                        })
                    }
                })
            }
        })
    }
    func showLevelNode() {
        let levelNode = self.gameNode.childNode(withName: "level", recursively: false)!
        let animateQueue = [["test1"],
                            ["test2", "test6"],
                            ["test3", "test7", "test11"],
                            ["test4", "test8", "test12", "test16"],
                            ["test5", "test9", "test13", "test17", "test21"],
                            ["test10", "test14", "test18", "test22"],
                            ["test15", "test19", "test23"],
                            ["test20", "test24"],
                            ["test25"]]
        delay(delay: 0.5, closure: {
            for i in 0 ..< animateQueue.count {
                delay(delay: 1.0 * Double(i) / Double(animateQueue.count), closure: {
                    for j in 0 ..< animateQueue[i].count {
                        let node = levelNode.childNode(withName: animateQueue[i][j], recursively: false)!
                        node.position.y = 0.0
                        SCNTransaction.animateWithDuration(0.5, completionBlock: {
                            SCNTransaction.animateWithDuration(0.1) {
                                node.position.y += 0.2
                            }
                        }, animations: {
                            node.position.y -= 1.2
                            node.opacity = 1.0
                        })
                    }
                })
            }
        })
    }
    func flip(direction: UISwipeGestureRecognizer.Direction, completionBlock: (() -> Void)? = nil) {
        SCNTransaction.animateWithDuration(0.5, completionBlock: {
            self.sceneView.isUserInteractionEnabled = true
            self.sceneState = self.sceneState == .welcome ? .levelControl : .welcome
            completionBlock?()
        }) {
            self.sceneView.isUserInteractionEnabled = false
            
            //根据swipe方向翻转
            if direction == .up {
                self.cameraXHandle.rotation.w += Float.pi
            }
            else if direction == .down {
                self.cameraXHandle.rotation.w -= Float.pi
            }
            
            if self.sceneState == .levelControl {
                self.hideLevelNode()
                self.sceneView.pointOfView!.position = SCNVector3(0.0, 0.0, self.configurator.welcomeCameraHeight)
            } else if self.sceneState == .welcome {
                self.showLevelNode()
                self.sceneView.pointOfView!.position = SCNVector3(0.0, 0.0, 15)
            }
        }
    }
    @objc func swipeGesture(_ sender: UISwipeGestureRecognizer) {
        guard self.sceneState != .playGame else { return }
        if sender.direction.contains(.up) {
            self.flip(direction: .up)
        }
        else {
            self.flip(direction: .down)
        }
    }
}
